using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Treeerarh
{
    public class Options
    {
        public bool ShowCheckBoxes;
        public bool LoadLastFile;
        public bool AutoSaveFile;
        public bool saveOnExit;
        public string LastFileName;
        public List<String> ResentFiles;
        public Options()
        {
                
        }

        public static Options Load()
        {
            Options tmp=new Options();
            try
            {
            XmlSerializer serializer = new XmlSerializer(typeof(Options));

            // A FileStream is needed to read the XML document.
            FileStream fs = new FileStream(OptionsFile(), FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);

            // Declare an object variable of the type to be deserialized.
            
            // Use the Deserialize method to restore the object's state.
            tmp=  (Options)serializer.Deserialize(reader);
            fs.Close();
            if (tmp.ResentFiles == null)
                tmp.ResentFiles = new List<string>();
            }
            catch (Exception ex)
            {
                tmp.ShowCheckBoxes = true;
                tmp.LoadLastFile = true;
                tmp.AutoSaveFile = false;
                tmp.saveOnExit = true;
                tmp.LastFileName = "";
                tmp.ResentFiles = new List<string>();
            }

            return tmp;

        }
        public void Save()
        {

            XmlSerializer serializer =
      new XmlSerializer(typeof(Options));
            // Create an XmlTextWriter using a FileStream.
            Stream fs = new FileStream(OptionsFile(), FileMode.Create);
            XmlWriter writer =
            new XmlTextWriter(fs, Encoding.Unicode);
            // Serialize using the XmlTextWriter.
            serializer.Serialize(writer, this);
            writer.Close();
        }

        public void AddResentFile(string fileName)
        {
            try
            {
                if (!ResentFiles.Contains(fileName))
                    ResentFiles.Add(fileName);
            }
            catch (Exception) { }
        }

        private static string OptionsFile()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase + ".cnfg";
        }
    }
}
